/**   
* @Title: JobController.java
* @Package org.xaoyaoyao.cloud.job.controller
* @Description: TODO
* @author xaoyaoyao
* @date 2018年9月7日 下午1:44:27
* @version V1.0   
*/
package org.xaoyaoyao.cloud.job.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xaoyaoyao.cloud.job.entity.QuartzEntity;
import org.xaoyaoyao.cloud.job.result.Result;
import org.xaoyaoyao.cloud.job.serivce.JobService;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: JobController
 * @Description: TODO
 * @author xaoyaoyao
 * @date 2018年9月7日 下午1:44:27
 * 
 */
@Slf4j
@RestController
@RequestMapping("/job")
public class JobController {

	@Autowired
	private Scheduler scheduler;

	@Autowired
	private JobService jobService;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@PostMapping("/add")
	public Result save(QuartzEntity quartz) {
		log.info(">>>>>>>> 新增任务");
		try {
			// 如果是修改 展示旧的 任务
			if (quartz.getOldJobGroup() != null) {
				JobKey key = new JobKey(quartz.getOldJobName(), quartz.getOldJobGroup());
				scheduler.deleteJob(key);
			}
			Class cls = Class.forName(quartz.getJobClassName());
			cls.newInstance();
			// 构建job信息
			JobDetail job = JobBuilder.newJob(cls).withIdentity(quartz.getJobName(), quartz.getJobGroup()).withDescription(quartz.getDescription()).build();
			// 触发时间点
			CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartz.getCronExpression());
			Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger" + quartz.getJobName(), quartz.getJobGroup()).startNow().withSchedule(cronScheduleBuilder).build();
			// 交由Scheduler安排触发
			scheduler.scheduleJob(job, trigger);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return Result.error();
		}
		return Result.ok();
	}

	@PostMapping("/list")
	public Result list(QuartzEntity quartz, Integer pageNo, Integer pageSize) {
		log.info(">>>>>>>> 任务列表");
		List<QuartzEntity> list = jobService.listQuartzEntity(quartz, pageNo, pageSize);
		return Result.ok(list);
	}

	@PostMapping("/trigger")
	public Result trigger(QuartzEntity quartz, HttpServletResponse response) {
		log.info(">>>>>>>> 触发任务");
		try {
			JobKey key = new JobKey(quartz.getJobName(), quartz.getJobGroup());
			scheduler.triggerJob(key);
		} catch (SchedulerException e) {
			log.error(e.getMessage(), e);
			return Result.error();
		}
		return Result.ok();
	}

	@PostMapping("/pause")
	public Result pause(QuartzEntity quartz, HttpServletResponse response) {
		log.info(">>>>>>>> 停止任务");
		try {
			JobKey key = new JobKey(quartz.getJobName(), quartz.getJobGroup());
			scheduler.pauseJob(key);
		} catch (SchedulerException e) {
			log.error(e.getMessage(), e);
			return Result.error();
		}
		return Result.ok();
	}

	@PostMapping("/resume")
	public Result resume(QuartzEntity quartz, HttpServletResponse response) {
		log.info(">>>>>>>> 恢复任务");
		try {
			JobKey key = new JobKey(quartz.getJobName(), quartz.getJobGroup());
			scheduler.resumeJob(key);
		} catch (SchedulerException e) {
			log.error(e.getMessage(), e);
			return Result.error();
		}
		return Result.ok();
	}

	@PostMapping("/remove")
	public Result remove(QuartzEntity quartz, HttpServletResponse response) {
		log.info("移除任务");
		try {
			TriggerKey triggerKey = TriggerKey.triggerKey(quartz.getJobName(), quartz.getJobGroup());
			// 停止触发器
			scheduler.pauseTrigger(triggerKey);
			// 移除触发器
			scheduler.unscheduleJob(triggerKey);
			// 删除任务
			scheduler.deleteJob(JobKey.jobKey(quartz.getJobName(), quartz.getJobGroup()));
			log.debug(">>>>>>>> removeJob >> {}", JobKey.jobKey(quartz.getJobName()));
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return Result.error();
		}
		return Result.ok();
	}
}
